package edu.bbu.license.client.android.communication.services.thrift.directories;

import java.util.ArrayList;

import org.apache.thrift.TException;

import edu.bbu.license.client.android.communication.exceptions.DataBaseErrorException;
import edu.bbu.license.client.android.communication.exceptions.DirectoryOperationException;
import edu.bbu.license.client.android.communication.exceptions.NoConnectionException;
import edu.bbu.license.client.android.communication.exceptions.WrongUserDataException;
import edu.bbu.license.client.android.communication.factory.thrift.ThriftCLTFactory;
import edu.bbu.license.client.android.communication.model.DirectoryPOJO;
import edu.bbu.license.client.android.communication.services.DirectoryCLTServices;
import edu.bbu.license.client.android.communication.services.thrift.exceptions.TDatabaseErrorException;
import edu.bbu.license.client.android.communication.services.thrift.exceptions.TDirectoryOperationException;
import edu.bbu.license.client.android.communication.services.thrift.exceptions.TWrongUserDataException;
import edu.bbu.license.client.android.communication.utils.ThriftExecutor;

/**
 * The thrift direcotry services
 */
public class ThriftDirectoryCLTServices implements DirectoryCLTServices
{
	/**
	 * Creates a new directory.
	 */
	public void createDirectory(DirectoryPOJO directoryPOJO) throws NoConnectionException, DirectoryOperationException, DataBaseErrorException
	{
		ThriftDirectory thriftDirectory = new ThriftDirectory();
		thriftDirectory.setOwner(directoryPOJO.getOwner());
		thriftDirectory.setDirectoryName(directoryPOJO.getDirectoryName());
		thriftDirectory.setParrentDirectory(directoryPOJO.getParrentDirectory());
		
		ThriftExecutor thriftExecutor = ThriftCLTFactory.getExecutor();
		
		try
		{
			thriftExecutor.open();
			thriftExecutor.getExecutor().createDirectory(thriftDirectory);
		}
		catch (TDirectoryOperationException e)
		{
			throw new DirectoryOperationException("Directory already exist.");
		}
		catch (TDatabaseErrorException e)
		{
			throw new DataBaseErrorException("Error occured in database.");
		}
		catch (TException e)
		{
			throw new NoConnectionException("Connection lost.");
		}
		finally
		{
			thriftExecutor.close();
		}
	}

	/**
	 * Deletes the directory.
	 */
	public void deleteDirectory(DirectoryPOJO directoryPOJO) throws NoConnectionException, DirectoryOperationException, DataBaseErrorException
	{
		ThriftDirectory thriftDirectory = new ThriftDirectory();
		thriftDirectory.setOwner(directoryPOJO.getOwner());
		thriftDirectory.setDirectoryName(directoryPOJO.getDirectoryName());
		thriftDirectory.setParrentDirectory(directoryPOJO.getParrentDirectory());
		
		ThriftExecutor thriftExecutor = ThriftCLTFactory.getExecutor();
		
		try
		{
			thriftExecutor.open();
			thriftExecutor.getExecutor().deleteDirectory(thriftDirectory);
			
		}
		catch (TDirectoryOperationException e)
		{
			throw new DirectoryOperationException("Directory already deleted.");
		}
		catch (TDatabaseErrorException e)
		{
			throw new DataBaseErrorException("Error occured in database.");
		}
		catch (TException e)
		{
			throw new NoConnectionException("Connection lost.");
		}
		finally
		{
			thriftExecutor.close();
		}
	}

	/**
	 * Returns the directory
	 */
	public ArrayList<String> getDirectoriesFromDirectory(DirectoryPOJO directoryPOJO) throws NoConnectionException, DataBaseErrorException
	{
		ThriftDirectory thriftDirectory = new ThriftDirectory();
		thriftDirectory.setOwner(directoryPOJO.getOwner());
		thriftDirectory.setDirectoryName(directoryPOJO.getDirectoryName());
		thriftDirectory.setParrentDirectory(directoryPOJO.getParrentDirectory());
		
		ThriftExecutor thriftExecutor = ThriftCLTFactory.getExecutor();
		
		try
		{
			thriftExecutor.open();
			return (ArrayList<String>) thriftExecutor.getExecutor().getDirectoriesFromDirectory(thriftDirectory);
		}
		catch (TDatabaseErrorException e)
		{
			throw new DataBaseErrorException("Error occured in database.");
		}
		catch (TException e)
		{
			throw new NoConnectionException("Connection lost.");
		}
		finally
		{
			thriftExecutor.close();
		}
		
	}

	/**
	 * It gets the base directory.
	 */
	public DirectoryPOJO getBaseDirectory(String owner) throws NoConnectionException
	{	
		ThriftExecutor thriftExecutor = ThriftCLTFactory.getExecutor();
		
		try
		{
			thriftExecutor.open();
			ThriftDirectory thriftDirectory = thriftExecutor.getExecutor().getBaseDirectory(owner);
			
			DirectoryPOJO directoryPOJO = new DirectoryPOJO();			
			directoryPOJO.setOwner(thriftDirectory.getOwner());
			directoryPOJO.setDirectoryName(thriftDirectory.getDirectoryName());
			directoryPOJO.setParrentDirectory(thriftDirectory.getParrentDirectory());
			
			return directoryPOJO;
		}
		catch (TException e)
		{
			throw new NoConnectionException("Connection lost."); 
		}

		finally
		{
			thriftExecutor.close();
		}
	}

	/**
	 * It sets the synchcronizing ditectory.
	 */
	public void setSyncDirectory(DirectoryPOJO directory) throws NoConnectionException, DataBaseErrorException
	{
		ThriftDirectory thriftDirectory = new ThriftDirectory();
		thriftDirectory.setOwner(directory.getOwner());
		thriftDirectory.setDirectoryName(directory.getDirectoryName());
		thriftDirectory.setParrentDirectory(directory.getParrentDirectory());
		
		ThriftExecutor thriftExecutor = ThriftCLTFactory.getExecutor();
		
		try
		{			
			thriftExecutor.open();
			thriftExecutor.getExecutor().setSyncDirectory(thriftDirectory);
		}
		catch (TDatabaseErrorException e)
		{
			throw new DataBaseErrorException("Error occured in database.");
		}
		catch (TException e)
		{
			throw new NoConnectionException("Connection lost."); 
		}

		finally
		{
			thriftExecutor.close();
		}
		
	}

	/**
	 * It gets the sunchronizing directory.
	 */
	public DirectoryPOJO getSyncDirectory(String owner) throws NoConnectionException, WrongUserDataException,DataBaseErrorException
	{
		ThriftExecutor thriftExecutor = ThriftCLTFactory.getExecutor();
		
		try
		{
			thriftExecutor.open();
			ThriftDirectory thriftDirectory = thriftExecutor.getExecutor().getSyncDirectory(owner);
			
			DirectoryPOJO directoryPOJO = new DirectoryPOJO();			
			directoryPOJO.setOwner(thriftDirectory.getOwner());
			directoryPOJO.setDirectoryName(thriftDirectory.getDirectoryName());
			directoryPOJO.setParrentDirectory(thriftDirectory.getParrentDirectory());
			
			return directoryPOJO;
		}
		catch (TWrongUserDataException e)
		{
			throw new WrongUserDataException("Username not exist.");
		}
		catch (TDatabaseErrorException e)
		{
			throw new DataBaseErrorException("Error occured in database.");
		}
		catch (TException e)
		{
			throw new NoConnectionException("Connection lost."); 
		}

		finally
		{
			thriftExecutor.close();
		}
	}
}
